home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-02 / splotch.zip / SPLOTCH.PAS < prev    next >
Pascal/Delphi Source File  |  1991-09-29  |  6KB  |  292 lines

  1. program Splotch;
  2.  
  3.  
  4. {***************************************************************************
  5.                                 | SPLOTCH.pas  by Bill Reamy |
  6.                                 +----------------------------+
  7.  
  8.    An example of cellular automata.
  9.    note: unlike "Life" and many other examples, Splotch has changes that
  10.          occur one at a time, instead of an entrie 'generation' at a time.
  11.  
  12.    This program is loosly based on "Vote", ( no I don't remember who wrote
  13.    it, I just remember reading about it in a computer magazine).
  14.  
  15. ***************************************************************************}
  16.  
  17.  
  18. uses
  19.   Graph, CRT;
  20.  
  21.  
  22. var
  23.   Dummy : char;
  24.  
  25.   X, Y,
  26.   C, R  : integer;
  27.  
  28.  
  29. type
  30.   ColorValue = record
  31.                  RValue, GValue, BValue : byte;
  32.                end;
  33.  
  34.  
  35. var
  36.   VgaPalette  : array[0..255] of ColorValue;
  37.  
  38.  
  39.  
  40.  
  41. {$F+}
  42. function FakeDet : integer;
  43. begin
  44.   FakeDet := 0;
  45. end;
  46. {$F-}
  47.  
  48.  
  49.  
  50.  
  51. procedure VgaSetAllPalette;
  52. var
  53.   Count : integer;
  54.  
  55. begin
  56.   for Count := 0 to 255
  57.     do begin
  58.        Port[$03C8] := Count;
  59.        Port[$03C9] := VgaPalette[Count].RValue and 63;
  60.        Port[$03C9] := VgaPalette[Count].GValue and 63;
  61.        Port[$03C9] := VgaPalette[Count].BValue and 63;
  62.        end;
  63.  
  64. end;
  65.  
  66.  
  67.  
  68.  
  69. procedure VgaSetPalette;
  70. var
  71.   Count : integer;
  72.  
  73. begin
  74.   for Count := 1 to 255
  75.     do begin
  76.        Port[$03C8] := Count;
  77.        Port[$03C9] := VgaPalette[Count].RValue and 63;
  78.        Port[$03C9] := VgaPalette[Count].GValue and 63;
  79.        Port[$03C9] := VgaPalette[Count].BValue and 63;
  80.        end;
  81.  
  82. end;
  83.  
  84.  
  85.  
  86.  
  87. procedure RGBPalette;
  88. var C :integer;
  89. begin
  90.   for C := 0 to 63
  91.     do begin
  92.        VgaPalette[C].RValue := C;
  93.        VgaPalette[C].GValue := C;
  94.        VgaPalette[C].BValue := C;
  95.        end;
  96.   for C := 64 to 127
  97.     do begin
  98.        VgaPalette[C].RValue := C;
  99.        VgaPalette[C].GValue := 0;
  100.        VgaPalette[C].BValue := 0;
  101.        end;
  102.   for C := 128 to 191
  103.     do begin
  104.        VgaPalette[C].RValue := 0;
  105.        VgaPalette[C].GValue := C;
  106.        VgaPalette[C].BValue := 0;
  107.        end;
  108.   for C := 192 to 255
  109.     do begin
  110.        VgaPalette[C].RValue := 0;
  111.        VgaPalette[C].GValue := 0;
  112.        VgaPalette[C].BValue := C;
  113.        end;
  114.   VGASetAllPalette;
  115. end;
  116.  
  117.  
  118.  
  119.  
  120. procedure MultiPalette;
  121. var C : integer;
  122. begin
  123.   for C := 0 to 31
  124.     do begin VgaPalette[C].RValue := (c and 31)*2;
  125.              VgaPalette[C].GValue := (c and 31)*2;       { Gray       }
  126.              VgaPalette[C].BValue := (c and 31)*2;
  127.        end;
  128.   for C := 32 to 63
  129.     do begin VgaPalette[c].RValue := (c and 31)*2;
  130.              VgaPalette[c].GValue := 0;                   { Red        }
  131.              VgaPalette[c].BValue := 0;
  132.        end;
  133.   for C := 64 to 95
  134.     do begin VgaPalette[c].RValue := 0;
  135.              VgaPalette[c].GValue := (c and 31)*2;       { Green      }
  136.              VgaPalette[c].BValue := 0;
  137.        end;
  138.   for C := 96 to 127
  139.     do begin VgaPalette[c].RValue := 0;
  140.              VgaPalette[c].GValue := 0;                    { Blue       }
  141.              VgaPalette[c].BValue := (c and 31)*2;
  142.        end;
  143.   for C := 128 to 159
  144.     do begin VgaPalette[c].RValue := (c and 31)*2;
  145.              VgaPalette[c].GValue := (c and 31)*2;        { Gold       }
  146.              VgaPalette[c].BValue := 0;
  147.        end;
  148.   for C := 160 to 191
  149.     do begin VgaPalette[c].RValue := (c and 31)*2;
  150.              VgaPalette[c].GValue := 0;                    { Purple     }
  151.              VgaPalette[c].BValue := (c and 31)*2;
  152.        end;
  153.   for C := 192 to 223
  154.     do begin VgaPalette[c].RValue := 0;
  155.              VgaPalette[c].GValue := (c and 31)*2;        { Cyan       }
  156.              VgaPalette[c].BValue := (c and 31)*2;
  157.        end;
  158.   for C := 224 to 255
  159.     do begin VgaPalette[c].RValue := ((c and 31)shr 2)*2;
  160.              VgaPalette[c].GValue := ((c and 31)shr 1)*2;    { Steel Blue }
  161.              VgaPalette[c].BValue := (c and 31)*2;
  162.        end;
  163.   VgaPalette[0].RValue := 0;
  164.   VgaPalette[0].GValue := 0;
  165.   VgaPalette[0].BValue := 0;
  166.   VgaSetAllPalette;
  167. end;
  168.  
  169.  
  170.  
  171.  
  172. procedure GrayPalette;
  173. var
  174.   C : integer;
  175. begin
  176.   for C := 0 to 256
  177.     do begin
  178.        VgaPalette[C].RValue := C;
  179.        VgaPalette[C].GValue := C;
  180.        VgaPalette[C].BValue := C;
  181.        end;
  182.   VgaSetAllPalette;
  183. end;
  184.  
  185.  
  186.  
  187.  
  188. procedure RandPalette;
  189. var
  190.   C,R,G,B : integer;
  191. begin
  192.   R := Random(64);
  193.   G := Random(64);
  194.   B := Random(64);
  195.   for C := 0 to 255
  196.     do begin
  197.        VgaPalette[C].RValue := R+C;
  198.        VgaPalette[C].GValue := G+C*2;
  199.        VgaPalette[C].BValue := B+C*3;
  200.        end;
  201.   VGASetAllPalette;
  202. end;
  203.  
  204.  
  205.  
  206.  
  207. procedure NextPalette;
  208. var
  209.   Count : integer;
  210.   T1, T2, T3 : byte;
  211. begin
  212.   T1 := VgaPalette[1].RValue;
  213.   T2 := VgaPalette[1].GValue;
  214.   T3 := VgaPalette[1].BValue;
  215.   for Count := 2 to 255 do
  216.     VgaPalette[Count-1] := VgaPalette[Count];
  217.   VgaPalette[255].RValue := T1;
  218.   VgaPalette[255].GValue := T2;
  219.   VgaPalette[255].BValue := T3;
  220.   VgaSetPalette;
  221. end;
  222.  
  223.  
  224.  
  225.  
  226. procedure Init;
  227. var
  228.   Gd, Gm : integer;
  229. begin
  230.   DetectGraph( Gd, Gm );
  231.   if Gd <> VGA
  232.     then begin
  233.          Writeln( 'Sorry, SPLOTCH requires VGA.' );
  234.          HALT(1);
  235.          end;
  236.   if InstallUserDriver( 'Vga256', @FakeDet ) = grError
  237.     then HALT(1);
  238.   Gd := Detect;
  239.   InitGraph( Gd, Gm, '' );
  240.   if GraphResult <> GrOK
  241.     then begin
  242.          Writeln( 'Error in SPLOTCH.exe: Not Enough Free Memory!' );
  243.          HALT(1);
  244.          end;
  245.   MultiPalette;
  246.   Randomize;
  247. end;
  248.  
  249.  
  250.  
  251.  
  252. begin
  253.   Init;
  254.  
  255.   for X := 0 to 319 do
  256.     for Y := 0 to 199 do
  257.       PutPixel( X, Y, Random(256) );
  258.  
  259.   while not KeyPressed
  260.     do begin
  261.        X := Random(320);
  262.        Y := Random(200);
  263.        C := GetPixel(X,Y);
  264.        R := Random(3);
  265.        if R = 0
  266.          then Inc(X)
  267.          else if R = 1
  268.                 then Dec(X);
  269.        R := Random(3);
  270.        if R = 0
  271.          then Inc(Y)
  272.          else if R = 1
  273.                 then Dec(Y);
  274.        if X < 0
  275.          then X := 319
  276.          else if X > 319
  277.                 then X := 0;
  278.  
  279.        if Y < 0
  280.          then Y := 199
  281.          else if Y > 199
  282.                 then Y := 0;
  283.  
  284.        PutPixel( X,Y, (C + GetPixel(X,Y)) div 2 );
  285.        if (Mem[$0040:$0017] and $10) > 0
  286.          then NextPalette;
  287.        end;
  288.  
  289.   Dummy := ReadKey;
  290.   CloseGraph;
  291. end.
  292.